﻿using XmWpfBase.Components;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using System.Linq;
using XmWpfBase.Models;
using XmWpfBase.Views;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using XmWpfBase.Common;
using Path = System.IO.Path;

namespace XmWpfBase 
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : WindowSkin
    {
        private MainWindowModel _model;
        private bool _isAllSelected = false;

        public MainWindow()
        {
            InitializeComponent();
            _model = new MainWindowModel();
            DataContext = _model;

            this.Loaded += MainWindow_Loaded;
        }

        private void OnMinimizeButtonClick(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void OnMaximizeButtonClick(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
            }
            else
            {
                WindowState = WindowState.Maximized;
            }
        }

        private void OnCloseButtonClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void OnTitleBarMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (e.ClickCount == 2)
                {
                    // 双击处理
                    if (this.WindowState == WindowState.Maximized)
                    {
                        this.WindowState = WindowState.Normal;
                    }
                    else
                    {
                        this.WindowState = WindowState.Maximized;
                    }
                    e.Handled = true;  // 标记事件已处理，防止触发 DragMove
                }
                else
                {
                    // 单击处理
                    this.DragMove();
                }
            }
        }

        private void OnTitleBarDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.WindowState = WindowState.Normal;
            }
            else
            {
                this.WindowState = WindowState.Maximized;
            }
        }

        private void OnOpenDialogClick(object sender, RoutedEventArgs e)
        {
            var dialog = new DialogWindow();
            dialog.Title = "示例弹窗";
            
            // 创建弹窗内容
            var content = new StackPanel
            {
                Margin = new Thickness(20)
            };
            
            // 添加标题
            content.Children.Add(new TextBlock 
            { 
                Text = "这是一个示例弹窗",
                FontSize = 16,
                Margin = new Thickness(0, 0, 0, 20)
            });
            
            // 添加按钮
            var buttonPanel = new StackPanel
            {
                Orientation = Orientation.Horizontal,
                HorizontalAlignment = HorizontalAlignment.Right
            };
            
            var cancelButton = new XmButton
            {
                ButtonText = "取消",
                Type = XmButtonType.Text,
                PrimaryColor = "#FF08979C",
                Margin = new Thickness(0, 0, 10, 0)
            };
            cancelButton.Click += (s, args) => dialog.Close();
            
            var confirmButton = new XmButton
            {
                ButtonText = "确定",
                Type = XmButtonType.Fill,
                PrimaryColor = "#FF08979C"
            };
            confirmButton.Click += (s, args) => dialog.Close();
            
            buttonPanel.Children.Add(cancelButton);
            buttonPanel.Children.Add(confirmButton);
            content.Children.Add(buttonPanel);
            
            // 设置弹窗内容
            dialog.Content = content;
            dialog.ResizeMode = ResizeMode.NoResize;
            dialog.MaximizeVisibility = Visibility.Hidden;
            dialog.MinimizeVisibility = Visibility.Hidden;

            // 显示弹窗
            dialog.Width = 300;
            dialog.Height = 185;
            dialog.ShowDialog();
        }

        private void OnOpenFormDialogClick(object sender, RoutedEventArgs e)
        {
            var dialog = new DialogWindow();
            dialog.Title = "表单对话框";
            
            // 创建表单内容
            var form = new FormPage();
            dialog.Content = form;
            
            // 设置对话框属性
            dialog.ResizeMode = ResizeMode.NoResize;
            dialog.MaximizeVisibility = Visibility.Hidden;
            dialog.MinimizeVisibility = Visibility.Hidden;
            
            // 设置对话框大小
            dialog.Width = 400;
            dialog.Height = 340;
            
            // 显示对话框
            dialog.ShowDialog();
        }
    }
}